package com.md.client;

import com.md.api.OpenSystemApi;
import com.md.client.helper.DefaultOpenSystemApiController;
import com.md.request.ApiRequest;
import com.md.request.QueryAppInfoRequest;
import com.md.request.QueryModuleRequest;
import com.md.response.ApiResponse;
import com.md.response.AppInfoResponse;
import com.md.response.ModuleResponse;
import com.md.util.SpringContextUtil;
import com.md.value.enums.ApiMethodEnum;
import lombok.experimental.UtilityClass;

import java.util.List;

/**
 * @author 刘骄阳
 * 2024-07-14 22:37
 */
@UtilityClass
public class OpenApiClient {

  private OpenSystemApi openSystemApi;

  private static OpenSystemApi getOpenSystemApi() {
    if (openSystemApi == null) {
      openSystemApi = SpringContextUtil.getBean(DefaultOpenSystemApiController.class);
    }
    return openSystemApi;
  }

  public static AppInfoResponse queryAppInfo(final String appCode) {
    QueryAppInfoRequest request = new QueryAppInfoRequest();
    request.setAppCode(appCode);
    return getOpenSystemApi().openQuery(request).getData();
  }

  public static List<AppInfoResponse> queryAppInfoByModuleId(final Long moduleId) {
    QueryAppInfoRequest request = new QueryAppInfoRequest();
    request.setModuleId(moduleId);
    return getOpenSystemApi().openQueryListByModuleId(request).getData();
  }

  public static AppInfoResponse queryAppInfoById(final Long id) {
    QueryAppInfoRequest request = new QueryAppInfoRequest();
    request.setId(id);
    return getOpenSystemApi().openQuery(request).getData();
  }

  public static AppInfoResponse queryAppInfo(final Long tenantId, final Long brandId) {
    QueryAppInfoRequest request = new QueryAppInfoRequest();
    request.setTenantId(tenantId);
    request.setBrandId(brandId);
    return getOpenSystemApi().openQuery(request).getData();
  }

  public static List<AppInfoResponse> queryAppInfoList(final String appCode) {
    QueryAppInfoRequest request = new QueryAppInfoRequest();
    request.setAppCode(appCode);
    return getOpenSystemApi().openQueryList(request).getData();
  }

  public static List<AppInfoResponse> queryAppInfoList(final Long tenantId, final Long brandId) {
    QueryAppInfoRequest request = new QueryAppInfoRequest();
    request.setTenantId(tenantId);
    request.setBrandId(brandId);
    return getOpenSystemApi().openQueryList(request).getData();
  }

  public static void openCreateApi(final ApiRequest request) {
    getOpenSystemApi().openCreateApi(request);
  }

  public static List<ApiResponse> queryApiList(final ApiRequest request) {
    return getOpenSystemApi().openQueryList(request).getData();
  }

  public static ApiResponse queryApi(final ApiRequest request) {
    return getOpenSystemApi().openQuery(request).getData();
  }

  public static List<ApiResponse> queryApi(final Long appInfoId) {
    ApiRequest request = new ApiRequest();
    request.setAppInfoId(appInfoId);
    return getOpenSystemApi().openQueryList(request).getData();
  }

  public static ApiResponse queryApi(final String url, final String method) {
    ApiRequest request = new ApiRequest();
    request.setUrl(url);
    request.setMethod(ApiMethodEnum.of(method.toUpperCase()));
    return getOpenSystemApi().openQueryByUrlMethod(request).getData();
  }

  public static List<ModuleResponse> queryModuleList(final QueryModuleRequest request) {
    return getOpenSystemApi().openQueryList(request).getData();
  }

  public static List<ModuleResponse> openQueryListByIdList(final List<Long> idList) {
    QueryModuleRequest request = new QueryModuleRequest();
    request.setIdList(idList);
    return getOpenSystemApi().openQueryListByIdList(request).getData();
  }

}
